package org.darcy.youchat.activity.message;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;

import org.darcy.youchat.imageloader.MyImageLoader;
import org.darcy.youchat.library.adapter.LQRAdapterForRecyclerView;
import org.darcy.youchat.library.adapter.LQRViewHolderForRecyclerView;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.team.model.Team;
import com.netease.nimlib.sdk.team.model.TeamMember;
import com.netease.nimlib.sdk.uinfo.model.NimUserInfo;

import org.darcy.youchat.R;
import org.darcy.youchat.activity.BaseActivity;
import org.darcy.youchat.databinding.ActivityTeamChatRemoveMemberBinding;
import org.darcy.youchat.model.UserCache;
import org.darcy.youchat.nimsdk.NimTeamSDK;
import org.darcy.youchat.nimsdk.NimUserInfoSDK;
import org.darcy.youchat.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 移除群成员界面
 */
public class TeamChatRemoveMemberActivity extends BaseActivity {

    private ActivityTeamChatRemoveMemberBinding binding;

    public static final String TEAMID = "teamId";
    public static final String REMOVE_TEAM_MEMBER = "remove_team_member";//踢人出群
    private String mTeamId;
    private Team mTeam;
    private List<TeamMember> mTeamMembers = new ArrayList<>();
    private ArrayList<String> mWillBeRemovedAccounts = new ArrayList<>();
    private LQRAdapterForRecyclerView<TeamMember> mAdapter;


    @Override
    public void init() {
        mTeamId = getIntent().getStringExtra(TEAMID);
        if (TextUtils.isEmpty(mTeamId)) {
            interrupt();
        }
        mTeam = NimTeamSDK.queryTeamBlock(mTeamId);
    }

    @Override
    public void initView() {
        binding = ActivityTeamChatRemoveMemberBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setSupportActionBar(binding.mToolbar.toolbar);
        getSupportActionBar().setTitle("移除群成员");
        binding.mToolbar.toolbar.setNavigationIcon(R.mipmap.ic_back);
        binding.mToolbar.btnOk.setVisibility(View.VISIBLE);

        binding.mToolbar.btnOk.setVisibility(View.VISIBLE);
        binding.mToolbar.btnOk.setText("删除");
        binding.mToolbar.btnOk.setBackgroundResource(R.drawable.shape_btn_delete);
        binding.mToolbar.btnOk.setOnClickListener(v -> {
            if (mWillBeRemovedAccounts.size() > 0) {
                Intent intent = new Intent();
                intent.putStringArrayListExtra(REMOVE_TEAM_MEMBER, mWillBeRemovedAccounts);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }

    @Override
    public void initData() {
        //获取群成员
        NimTeamSDK.queryMemberList(mTeamId, new RequestCallback<List<TeamMember>>() {
            @Override
            public void onSuccess(List<TeamMember> param) {
                mTeamMembers.clear();
                mTeamMembers.addAll(param);

                //调整群主位置置顶
                int creatorPosi = -1;
                for (int i = 0; i < param.size(); i++) {
                    TeamMember tm = param.get(i);
                    if (mTeam.getCreator().equals(tm.getAccount())) {
                        creatorPosi = i;
                        break;
                    }
                }
                if (creatorPosi != -1) {
                    mTeamMembers.remove(creatorPosi);
                    mTeamMembers.add(0, param.get(creatorPosi));
                }
                setAdapter();
            }

            @Override
            public void onFailed(int code) {
                UIUtils.showToast("获取群成员失败" + code);
            }

            @Override
            public void onException(Throwable exception) {
                exception.printStackTrace();
            }
        });
    }

    private void setAdapter() {
        if (mAdapter == null) {
            mAdapter = new LQRAdapterForRecyclerView<TeamMember>(this, R.layout.item_contact_cv, mTeamMembers) {
                @Override
                public void convert(LQRViewHolderForRecyclerView helper, final TeamMember item, int position) {
                    helper.setText(R.id.tvName, NimTeamSDK.getTeamMemberDisplayNameWithoutMe(item.getTid(), item.getAccount()));
                    ImageView ivHeader = helper.getView(R.id.ivHeader);
                    NimUserInfo userInfo = NimUserInfoSDK.getUser(item.getAccount());
                    if (userInfo != null && !TextUtils.isEmpty(userInfo.getAvatar())) {
                        MyImageLoader.LoadNetImage(userInfo.getAvatar(), ivHeader);
                    } else {
                        ivHeader.setImageResource(R.mipmap.default_header);
                    }
                    final CheckBox cb = helper.getView(R.id.cb);
                    if (UserCache.getAccount().equals(item.getAccount())) {
                        cb.setVisibility(View.GONE);
                    } else {
                        cb.setVisibility(View.VISIBLE);
                    }

                    helper.getView(R.id.root).setOnClickListener(v -> {
                        if (!UserCache.getAccount().equals(item.getAccount())) {
                            if (cb.isChecked()) {
                                cb.setChecked(false);
                                mWillBeRemovedAccounts.remove(item.getAccount());
                            } else {
                                cb.setChecked(true);
                                mWillBeRemovedAccounts.add(item.getAccount());
                            }
                        }
                    });
                }
            };
            binding.rvMember.setAdapter(mAdapter);
        } else {
            mAdapter.notifyDataSetChanged();
        }
    }

}
